//
//  ArticleCollectionViewCell.m
//  Project_A Gary
//
//  Created by leilei on 16/4/8.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "ArticleCollectionViewCell.h"
#import "ArticleTableViewCell.h"
#import "ArticleModel.h"


@interface ArticleCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableV;
@property (nonatomic, retain)NSMutableArray *dataArr;

@end


@implementation ArticleCollectionViewCell

-(void)dealloc
{
    [_articleId release];
    [_tableV release];
    [_dataArr release];
    [super dealloc];
    
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableV];
    }
    return self;
}

#pragma mark - 位置
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
}

#pragma mark - 数据解析实现的位置
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self dataHandle];
}


#pragma mark - 创建tableView
- (void)createTableV
{
    self.tableV = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableV];
    self.tableV.backgroundColor = [UIColor whiteColor];
    self.tableV.delegate = self;
    self.tableV.dataSource  =self;
    self.tableV.rowHeight = 120;
    
    
    [self.tableV registerClass:[ArticleTableViewCell class] forCellReuseIdentifier:@"ArticleTableViewCell"];
    
    [_tableV release];
}

#pragma mark - tableView的两个协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}


#pragma mark - cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArticleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ArticleTableViewCell" forIndexPath:indexPath];
    
    ArticleModel *aMOdel = self.dataArr[indexPath.row];
    cell.titleLab.text = aMOdel.title;
    cell.timeLab.text = aMOdel.pubDate;
    cell.countLab.text = [NSString stringWithFormat:@"评论数:%@", aMOdel.commentNum];
    //cell.backgroundColor = [UIColor colorWithRed:235 / 255.0 green:235 / 255.0 blue:235 / 255.0 alpha:1];
    
    return cell;
}




#pragma mark - 数据解析
- (void)dataHandle
{
    NSString *str = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v3/price/getNewestInfoListBySerialId/%@?pageNo=1&pageSize=20", self.articleId];
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        
        self.dataArr = [ArticleModel transformWithArray:responseObject[@"data"]];
        [self.tableV reloadData];
        
    } filed:^(NSError *error) {
        
    }];
    
}




@end
